var rootPath = getRootPath();

$(function() {
	//滚动条置顶
	 $("body").prepend('<input type="text" id="focus" size="1">');
    $("#focus").focus();
   $("#focus").hide();
	/*//获取当前页面高度，赋值给父页面的iframe
	var docheight = $(document).height();
	$(window.parent.document).find("#content").css("height",""+docheight+"px");*/
	var rleId = getUrlParam("rleId");
	$("#rleId").val(rleId);
	
	var prevPage = getUrlParam("currentPage");
	//设置select字体颜色变化
/*	$("select").each(function() {
		selectColor($(this));
		$(this).css("cursor", "pointer");
		$(this).change(function() {
			selectColor($(this));
		});
	});*/
	
	$("input[type='file']").each(function() {
		$(this).change(function() {
			$(this).siblings(".uploadFile").css("background","#EA9792");
			if($(this).val() == '') {
				$(this).siblings(".uploadFile").html("请上传");
			} else {			
				$(this).siblings(".uploadFile").html($(this)[0].files[0].name);
				$(this).siblings(".uploadFile").css("background","#B4B4B4");
				$(this).siblings(".msg").css("display","none");
			}
		});
	});
	
	
	$("#pic").uploadPreview({
		Img : "frontImg",
		Width : 100,
		Height : 100,
		addWaterMark : false
	});
	
	$("#pic").change(function() {
		if($(this).val() == '') {
			$("#frontImg").attr("src","");
			$("#fwSpan").removeClass("watermark");
			$("#fwSpan").html("请上传机构登记证书");
			showErrMsg("crePic", "请上传机构登记证书");
		} else {
			hideMsg("crePic");
		}
	});
	
	$("#picB").uploadPreview({
		Img : "frontImgB",
		Width : 100,
		Height : 100,
		addWaterMark : false
	});
	
	$("#picB").change(function() {
		if($(this).val() == '') {
			$("#frontImgB").attr("src","");
			$("#fwSpanB").removeClass("watermark");
			$("#fwSpanB").html("请上传证书");
			showErrMsg("crePicB", "请上传证书");
		} else {
			hideMsg("crePicB");
		}
	});
	
	//上一步
	$("#prev").click(function() {
		var creType = $("#sort").val();
		parent.$("#content").prop("src", prevPage + "?rleId=" + rleId + "&creType=" + creType);
	});	
	
	$("#applyAuth").click(function() {
		var sort = $("#sort").val();
		if(!sort) {
			layer.alert("数据出错了")
			return;
		}
		
		if(sort =="20"){
			//乡镇版
			if($.trim($("#Addr").val()) == '') {
				$("#Addr").focus();
				showErrMsg("Addr","请选择地区");
				return;
			}else{
				hideMsg("Addr");
			}
			var location = $("#Addr").data("code");
			if(!location || location==''){
				layer.msg("数据异常，即将刷新",{icon:5,time:1200},function(){
					window.location.reload();
					return;
				});
			}
			$("#location").val(location);
			
			if($.trim($("#company").val()) == '') {
				$("#company").focus();
				showErrMsg("company","请输入机构名称");
				return false;
			} else {
				hideMsg("company");
			}
			if ($("#company").val().length > 50) {
				$("#company").focus();
				showErrMsg("company","机构名称输入过长,请精简");
				return false;
			} else {
				hideMsg("company");
			}
			
			
			if(!existRecord && $("#picB").val() == '') {
				$("#pic").focus();
				showErrMsg("crePicB", "请上传证书");
				return;
			} 
		} else if(sort =="10"){
			$("#location").val("");
			if(!checkData()){
				return;
			}
		} else {
			layer.alert("数据出错了！")
			return;
		}
		showLoading();
		//adreamCenter
		$("#adreamCenterForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000') {
					layer.msg("操作成功",{icon:1,time:1000},function() {
						parent.$("#content").prop("src","applyResult.html?rleId=" + rleId + "&title=社区认证");
					});
				}else{
					layer.alert(data.returnMessage);
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("error...");
			}
		});
		return false;
	});
});

function checkData(){
	var cName =  $("#communityName").val();
	if(cName == undefined || cName == null || $.trim(cName) == '') {
		$("#communityName").focus();
		showErrMsg("communityName","请选择社区名称");
		return false;
	} else {
		hideMsg("communityName");
	}
	
	if($.trim($("#cacName").val()) == '') {
		$("#cacName").focus();
		showErrMsg("cacName","请输入社区梦想中心名称");
		return false;
	}else{
		hideMsg("cacName");
	}
	if($.trim($("#cacCode").val()) == '') {
		$("#cacCode").focus();
		showErrMsg("cacCode","请输入社区梦想中心编码");
		return false;
	}else{
		hideMsg("cacCode");
	}
	if ($("#cacCode").val().length > 14) {
		$("#cacCode").focus();
		showErrMsg("cacCode","请输入正确十四位梦想中心编码");
		return false;
	} else {
		hideMsg("cacCode");
	}
	
	if($.trim($("#company").val()) == '') {
		$("#company").focus();
		showErrMsg("company","请输入机构名称");
		return false;
	} else {
		hideMsg("company");
	}
	if ($("#company").val().length > 50) {
		$("#company").focus();
		showErrMsg("company","机构名称输入过长,请精简");
		return false;
	} else {
		hideMsg("company");
	}
	if(!existRecord && $("#pic").val() == '') {
		$("#pic").focus();
		showErrMsg("crePic", "请上传机构登记证书");
		return false;
	}  else {
		hideMsg("crePic");
	}
	if($.trim($("#contact").val()) == '') {
		$("#contact").focus();
		showErrMsg("contact","请输入联系人姓名");
		return false;
	} else {
		hideMsg("contact");
	}
	if ($("#contact").val().length > 50) {
		$("#contact").focus();
		showErrMsg("contact","联系人姓名输入过长,请精简");
		return;
	}else {
		hideMsg("contact");
	}
	if($.trim($("#phone").val()) == '') {
		$("#phone").focus();
		showErrMsg("phone","请输入手机号码");
		return false;
	} else {
		if (oriPhone != $("phone").val()) {
			if($("#phone").val().length > 11) {
				$("#phone").focus();
				showErrMsg("phone", "手机号码输入过长,请精简");
				return false;
			}
		}
		hideMsg("phone");
	}
	if($.trim($("#wechat").val()) == '') {
		$("#wechat").focus();
		showErrMsg("wechat","请输入微信账号");
		return false;
	} else {
		hideMsg("wechat");
	}
	if($("#wechat").val().length > 50) {
		$("#wechat").focus();
		showErrMsg("wechat", "微信账号输入过长");
		return false;
	} else {
		hideMsg("wechat");
	}
	return true;
}